package com.sduept.nwld.dataserver.manager.cim;

import java.util.Date;
import java.util.List;

import com.sduept.core.dao.SuperDAO;
import com.sduept.cim.ledger.total.model.PrimaryDeviceCount;
import com.sduept.utils.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 一次设备统计表查询Manager
 */
@Service
public class PrimaryDeviceCountQueryManager {

	@Autowired
	private SuperDAO dao;

	public static final byte dispatchType = 0;
	public static final byte maintenanceType = 1;
	/**
	 * 获取指定年份的一次设备统计
	 * @param year
	 * @return
	 */
	public List<PrimaryDeviceCount> findPrimaryDeviceCountByYear(int year) {
		Date start = getStartTime(year);
		Date end = getEndTime(year);
		return dao.findByCondition("statisticTime BETWEEN ? AND ?", new Object[] {start,end}, "ORDER BY unitName,voltageLevel", null, PrimaryDeviceCount.class);
	}


	/**
	 *	查询指定年份的调管单位统计结果
	 * @param year
	 * @return
	 */
	public List<PrimaryDeviceCount> findDispatchPrimaryDeviceCount(int year) {
		return commonOperate(year, PrimaryDeviceCountQueryManager.dispatchType);
	}

	/**
	 *	查询指定年份的调管单位统计结果
	 * @param year
	 * @param equipmentType PrimaryDeviceCountPersistenceManager.StatisticType.generatingunit.getType()
	 * @return
	 */
	public List<PrimaryDeviceCount> countByEquipmentType(int year,byte equipmentType) {
		Date start = getStartTime(year);
		Date end = getEndTime(year);
		return dao.nativeQuery(PrimaryDeviceCount.class,"SELECT voltageLevel,CAST(SUM(statisticCount) AS SIGNED) as statisticCount  FROM t_equipment_device_count WHERE statisticTime BETWEEN ? AND ? AND unitType = 0 AND statisticType = ?  GROUP BY voltageLevel  ORDER BY voltageLevel", new Object[] {start,end,equipmentType});
	}


	/**
	 *	查询指定年份的检修单位统计结果
	 * @param year
	 * @return
	 */
	public List<PrimaryDeviceCount> findMaintenancePrimaryDeviceCount(int year) {
		return commonOperate(year, PrimaryDeviceCountQueryManager.maintenanceType);
	}

	/**
	 * 公共操作，根据年份，类型查询
	 * @param year
	 * @param type
	 * @return
	 */
	private List<PrimaryDeviceCount> commonOperate(int year, byte type) {
		Date start = DateUtil.getDateByString(year + "-01-01 00:00:00", DateUtil.pattern_ymdhms);
		Date end = DateUtil.getDateByString(year + "-12-31 23:59:59", DateUtil.pattern_ymdhms);
		return dao.findByCondition("unitType=? AND statisticTime BETWEEN ? AND ? ",
				new Object[] { type, start, end }, "ORDER BY unitName,voltageLevel", null, PrimaryDeviceCount.class);

	}

	/**
	 * @param year
	 * @param voltage
	 * @return
	 */
	public List<PrimaryDeviceCount> findPrimaryDeviceCountByYearVoltage(int year, String[] voltage) {
		if(voltage==null || voltage.length==0) {
			return findPrimaryDeviceCountByYear(year);
		}
		Date start = getStartTime(year);
		Date end = getEndTime(year);
		String insql = "";
		for(int i=0;i<voltage.length;i++) {
			if(i==voltage.length-1) {
				insql+=""+voltage[i]+"";
			}else {
				insql+=""+voltage[i]+",";
			}
		}
		String esql ="statisticTime BETWEEN ? AND ? AND voltageLevel IN ("+insql+")";
		return dao.findByCondition(esql,new Object[] {start,end},"ORDER BY unitName,voltageLevel",null,PrimaryDeviceCount.class);
	}
	/**
	 * @param year
	 * @param voltage
	 * @return
	 */
	public List<PrimaryDeviceCount> findDispatchPrimaryDeviceCountByYearVoltage(int year, String[] voltage) {
		if(voltage==null || voltage.length==0) {
			return findPrimaryDeviceCountByYear(year);
		}
		Date start = getStartTime(year);
		Date end = getEndTime(year);
		String insql = "";
		for(int i=0;i<voltage.length;i++) {
			if(i==voltage.length-1) {
				insql+=""+voltage[i]+"";
			}else {
				insql+=""+voltage[i]+",";
			}
		}
		String esql ="unitType=? and statisticTime BETWEEN ? AND ? AND voltageLevel IN ("+insql+")";
		return dao.findByCondition(esql,new Object[] {PrimaryDeviceCountQueryManager.dispatchType,start,end},"ORDER BY unitName,voltageLevel",null,PrimaryDeviceCount.class);
	}
	/**
	 * 根据年份获得查询的起始时间
	 * @param year
	 * @return
	 */
	private Date getStartTime(int year) {
		return DateUtil.getDateByString(year+"-01-01", DateUtil.pattern_ymd);
	}
	/**
	 * 根据年份获得查询时间的结束时间
	 * @param year
	 * @return
	 */
	private Date getEndTime(int year) {
		return DateUtil.getDateByString((year)+"-12-31", DateUtil.pattern_ymd);
	}
	/**
	 * 根据一次设备统计dto的条件查询一次设备信息
	 * @param stationCount  统计数量
	 * @param deviceType  统计类型
	 * @param selectedDispatchDto  统计dto
	 */
	public PrimaryDeviceCount findPrimaryDeviceCountByDeviceTypeUnitUnitTypeVoltageLevel( Byte deviceType,String unit
			,Byte unitType,Float voltageLevel) {
		String sql = " statisticType = ? and unitName = ? and unitType  =? and voltageLevel =? ";
		Object[] obj = new Object[]{deviceType,unit,unitType,voltageLevel};
		List<PrimaryDeviceCount> list = dao.findByCondition(PrimaryDeviceCount.class, sql, obj);
		if(list.isEmpty()) {
			return null;
		}
		return list.get(0);
	}
}
